package com.darrenchan.leetcode;

import java.util.Arrays;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2020-03-27 20:29
 */
public class Q914 {
    public boolean hasGroupsSizeX(int[] deck) {
        int[] cnts = new int[10000];
        for (int i = 0; i < deck.length; i++) {
            cnts[deck[i]]++;
        }

        System.out.println(Arrays.toString(cnts));

        int res = -1;
        for (int i = 0; i < cnts.length; i++) {
            if(cnts[i] == 0) {
                continue;
            }

            if(res == -1) {
                res = cnts[i];
            } else {
                res = gcd(res, cnts[i]);
            }
        }

        return res >= 2;
    }

    private int gcd(int a, int b) {
        while(a != b) {
            if(a > b) {
                a -= b;
            } else {
                b -= a;
            }
        }

        return a;
    }

    public static void main(String[] args) {
        System.out.println(new Q914().hasGroupsSizeX(new int[]{1, 2, 3, 4, 4, 3, 2, 1}));
    }
}
